/*
 * HMC5883.c
 *
 */

#include "HMC5883.h"
#include "stm32f10x.h"
#include "i2c.h"
#include "tool.h"
#include <stdlib.h>
#include <stdio.h>
#include "memory.h"
#include "Console.h"
#include "string.h"

#define HMC5883_SAD_W   0x3C
#define HMC5883_SAD_R   0x3D

#define MULTI_RW		0x80


#define CRA_Reg			0x00
#define CRB_Reg			0x01
#define MR_Reg			0x02
#define Out_X_H			0x03
#define Out_X_L			0x04
#define Out_Z_H			0x05
#define Out_Z_L			0x06
#define Out_Y_H			0x07
#define Out_Y_L			0x08
#define SR_Reg			0x09
#define IDA_Reg			0x0A
#define IDB_Reg			0x0B
#define IDC_Reg			0x0C


typedef struct _LIS_INIT_TBL{
	unsigned char reg;
	unsigned char value;
}LIS_INIT_TBL;

const LIS_INIT_TBL HMC5883_Init_Tbl[] =
{
	//M
//r
//      { CRA_Reg,				0x10 },	//0x10 },
//	{ CRB_Reg,				0x60 },
//	{ MR_Reg,                               0x80 },
        { CRA_Reg,				0x10 },	//0x10 },
	{ CRB_Reg,				0x60 },
	{ MR_Reg,                               0x80 },

};


static int HMC5883_Read(unsigned char reg, char *buf, unsigned char len)
{
	if (len > 1)
		reg |= MULTI_RW;
	
	return I2C_Read(HMC5883_SAD_R, reg, (unsigned char *)buf, len);
}

/**********
static int HMC5883_Write(unsigned char reg, char *buf, unsigned char len)
{
	if (len > 1)
		reg |= MULTI_RW;
	
	return I2C_Write(HMC5883_SAD_W, reg, (unsigned char *)buf, len);
}
***********/

int HMC5883_Init(void)
{
	//unsigned char id;
	int i;
	
	I2C_HW_Init();
    	
   
    for (i = 0; i < (sizeof(HMC5883_Init_Tbl)/sizeof(LIS_INIT_TBL)); i++)
	{
		I2C_Write(HMC5883_SAD_W, HMC5883_Init_Tbl[i].reg, (unsigned char *)&HMC5883_Init_Tbl[i].value, 1);
	}

	return 0;
}

void HMC5883_GET_XYZ(short *arg)
{
	char buf[7];
	
	HMC5883_Read(MR_Reg, buf, 7);

	arg[0] = (short)((((unsigned short)buf[1]) << 8) | ((unsigned short)buf[2]&0x00FF));
	arg[1] = (short)((((unsigned short)buf[3]) << 8) | ((unsigned short)buf[4]&0x00FF));
	arg[2] = (short)((((unsigned short)buf[5]) << 8) | ((unsigned short)buf[6]&0x00FF));
}

//fenris 20110630  ==start==
const LIS_INIT_TBL HMC5883_Init_Tbl_Set[] =
{
	//M
    { MR_Reg,                 0x03 },
    { CRA_Reg,				0x00 },
    { CRB_Reg,                0x00 },
    { CRA_Reg,				0x10 },
	{ CRB_Reg,				0x60 },
	{ MR_Reg,                 0x00 },
    { MR_Reg,                 0x03 },
    { CRA_Reg,				0x11 },
	{ MR_Reg,                 0x00 },
};
const LIS_INIT_TBL HMC5883_Init_Tbl_Reset[] =
{
	//M
    { MR_Reg,                 0x03 },
    { CRA_Reg,				0x12 },
	{ MR_Reg,                 0x00 },
};
const LIS_INIT_TBL HMC5883_Init_Tbl_Normal[] =
{
	//M
    { MR_Reg,                 0x03 },
    { CRA_Reg,				  0x10 },
	{ MR_Reg,                 0x00 },
};

void HMC5883_M_Set(void)
{
	int i;
	
	for (i = 0; i < (sizeof(HMC5883_Init_Tbl_Set)/sizeof(LIS_INIT_TBL)); i++)
	{
    	I2C_Write(HMC5883_SAD_W, HMC5883_Init_Tbl_Set[i].reg, (unsigned char *)&HMC5883_Init_Tbl_Set[i].value, 1);
	}
}

void HMC5883_M_Reset(void)
{
	int i;
	
	for (i = 0; i < (sizeof(HMC5883_Init_Tbl_Reset)/sizeof(LIS_INIT_TBL)); i++)
	{
    	I2C_Write(HMC5883_SAD_W, HMC5883_Init_Tbl_Reset[i].reg, (unsigned char *)&HMC5883_Init_Tbl_Reset[i].value, 1);
	}
}

void HMC5883_M_Normal(void)
{
	int i;
	
	for (i = 0; i < (sizeof(HMC5883_Init_Tbl_Normal)/sizeof(LIS_INIT_TBL)); i++)
	{
    	I2C_Write(HMC5883_SAD_W, HMC5883_Init_Tbl_Normal[i].reg, (unsigned char *)&HMC5883_Init_Tbl_Normal[i].value, 1);
	}
}